package com.toe.order.webapi.controller;


import com.toe.common.restful.JsonResult;
import com.toe.order.service.OrderClassService;
import com.toe.pojo.dto.order.AddOrderClassDTO;
import com.toe.pojo.vo.order.OrderClassDetailsVO;
import com.toe.pojo.vo.order.OrderClassSimpleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/order-class")
@Api(tags = "课程订单模块")
public class OrderClassController {

    @Autowired
    OrderClassService orderClassService;

    @PostMapping("/add")
    @ApiOperation("添加新订单")
    public JsonResult<Long> addNew(@RequestBody AddOrderClassDTO addOrderClassDTO) {
        return JsonResult.ok(orderClassService.addNewOrder(addOrderClassDTO));
    }

    @GetMapping("/update/{orderId}")
    @ApiOperation("用户删除订单")
    @ApiImplicitParam(value = "订单id", name = "orderId", required = true,
            dataType = "long", example = "1")
    public JsonResult<Void> updateIsDelete(@PathVariable Long orderId) {
        orderClassService.deleteByOrderId(orderId);
        return JsonResult.ok();
    }

    @GetMapping("/{orderId}")
    @ApiOperation("通过订单id查询该订单")
    @ApiImplicitParam(value = "订单id", name = "orderId", required = true,
            dataType = "long", example = "1")
    public JsonResult<OrderClassDetailsVO> getOrderDetails(@PathVariable Long orderId) {
        return JsonResult.ok(orderClassService.getOrderDetails(orderId));
    }

    @GetMapping("/all/{userId}")
    @ApiOperation("该用户的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "long", example = "6"),
    })
    public JsonResult<List<OrderClassSimpleVO>> listAllOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderClassService
                .listAllOrderByUserId(userId, pageNum, pageSize));
    }

    @GetMapping("/cancel/{userId}")
    @ApiOperation("该用户取消的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "long", example = "6"),
    })
    public JsonResult<List<OrderClassSimpleVO>> listCancelOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderClassService
                .listCancelOrderByUserId(userId, pageNum, pageSize));
    }

    @GetMapping("/is-pay/{userId}")
    @ApiOperation("该用户已支付的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "long", example = "6"),
    })
    public JsonResult<List<OrderClassSimpleVO>> listPayOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderClassService
                .listPayOrderByUserId(userId, pageNum, pageSize));
    }

    @GetMapping("/not-pay/{userId}")
    @ApiOperation("该用户待支付的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "long", example = "6"),
    })
    public JsonResult<List<OrderClassSimpleVO>> listNotPayOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderClassService
                .listNotPayOrderByUserId(userId, pageNum, pageSize));
    }

    // http://40eca32.nat123.fun:50333/order-class/pay/35
    // http://localhost:10005/order-class/pay/3
    @GetMapping(value = "/pay/{orderId}")
    public void pay(
            @PathVariable Long orderId,
            String returnUrl,
            HttpServletResponse httpResponse
    ) {
        orderClassService.pay(orderId, returnUrl, httpResponse);
    }

    // http://40eca32.nat123.fun:50333/order-class/phonePay/9
    @GetMapping(value = "/phonePay/{orderId}")
    public void phonePay(@PathVariable Long orderId, HttpServletResponse httpResponse) {
        orderClassService.phonePay(orderId, httpResponse);
    }

    // http://ironknight.gnway.cc:61498/order-goods/appPay/31
    @GetMapping(value = "/appPay/{orderId}")
    public void appPay(@PathVariable Long orderId, HttpServletResponse httpResponse) {
        orderClassService.appPay(orderId, httpResponse);
    }

    @PostMapping("/returnUrl/{orderId}")
    public void returnUrl(@PathVariable Long orderId, HttpServletRequest request) {
        System.out.println("-----进入同步调用controller-----");
        orderClassService.returnUrl(orderId, request);
    }

//    // http://ironknight.gnway.cc:61498/order-goods/refund/16
//    @GetMapping("/refund/{orderId}")
//    public JsonResult<Void> refund(@PathVariable Long orderId) {
//        orderClassService.refund(orderId);
//        return JsonResult.ok();
//    }

}

